import {Component, ElementRef, Input, OnInit, ViewChild} from '@angular/core';
import {CommonPage} from '../common-page';
import {DhlTreeComponent} from '../../../shared/components/dhlcomponent/tree/dhl-tree.component';
import {DataPerFormComponent} from './form/data.per.form.component';

@Component({
  templateUrl: './data.per.component.html'
})
export class DataPerComponent extends CommonPage implements OnInit {


  dataPerRootCode = 'area_data_root';
  dataPerValue;

  parentName;
  parentId;
  confirmMsg
  checkedNodeId;
  checkedName = '区域数据根节点';
  checkedCode;
  checkedId;
  notOperate = true;

  @ViewChild('dataPerTree', {read: DhlTreeComponent}) dataPerTreeRef: DhlTreeComponent;
  @ViewChild('dataPerForm', {read: ElementRef}) dataPerFormRef: ElementRef;

  ngOnInit(): void {
  }

  nodeChangeClick(node) {
    if (node.length > 0) {
      this.notOperate = false;
      this.confirmMsg = `确认删除"${node[0].title}"?`
      this.checkedNodeId = node[0].id;
      this.checkedName = node[0].title;
      this.checkedCode = node[0].key;
      const parents = node[0].fatherLine;
      if (parents.length > 1) {
        this.parentName = parents[1].title;
      } else {
        this.parentName = '';
      }
      // this.parentName = .title;
      // this.checkedId = node[0].dataper.id;
    } else {
      this.notOperate = true;
      this.parentName = '';
      this.checkedName = '区域数据根节点'
      this.checkedCode = ''
      this.checkedNodeId = undefined
    }
  }

  add() {
    this.windowService.openWindow({
      submitUrl: `dataper/save`,
      component: DataPerFormComponent,
      data: {
        parentId: this.checkedNodeId,
        parentName: this.checkedName
      },
      title: '新建',
      afterOk: () => {
        this.dataPerTreeRef.fresh();
      }
    });

  }

  deleteNodeClick() {

  }
}


